package com.lgkj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lgkj.R;
import com.lgkj.bean.meitao.AdressBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.view.SwipeListLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class AdressAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;


    private Set<SwipeListLayout> sets = new HashSet();

    private List<AdressBean.DataBean> dataBeans;
    private CallBack callBack;

    public void setCallBack(CallBack back) {
        this.callBack = back;
    }

    public Set<SwipeListLayout> getSets() {
        return sets;
    }

    public AdressAdapter(Context context) {
        this.context = context;
        dataBeans = new ArrayList<>();
    }

    public void setDataBeans(List<AdressBean.DataBean> dataBeans) {
        this.dataBeans = dataBeans;
    }


    @Override
    public int getCount() {
        return dataBeans.size();
    }

    @Override
    public AdressBean.DataBean getItem(int i) {
        return dataBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_adress, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.sllMain.setOnSwipeStatusListener(new MyOnSlipStatusListener(
                viewHolder.sllMain));
        viewHolder.tvName.setText("姓名:" + dataBeans.get(i).getName());
        viewHolder.tvPhone.setText("电话:" + dataBeans.get(i).getPhone());
        viewHolder.tvAdress.setText("详细地址:" + dataBeans.get(i).getProvince_name() + "-" + dataBeans.get(i).getCity_name() + "-" + dataBeans.get(i).getRegion_name() + "-" + dataBeans.get(i).getAddress());

        if (dataBeans.get(i).getIs_default().equals("1")) {
            viewHolder.ivMoren.setVisibility(View.VISIBLE);
        } else {
            viewHolder.ivMoren.setVisibility(View.GONE);

        }

        viewHolder.tvTop.setOnClickListener(this);
        viewHolder.tvTop.setTag(i);
        viewHolder.tvDelete.setOnClickListener(this);
        viewHolder.tvDelete.setTag(i);
        return view;
    }

    class MyOnSlipStatusListener implements SwipeListLayout.OnSwipeStatusListener {

        private SwipeListLayout slipListLayout;

        public MyOnSlipStatusListener(SwipeListLayout slipListLayout) {
            this.slipListLayout = slipListLayout;
        }

        @Override
        public void onStatusChanged(SwipeListLayout.Status status) {
            if (status == SwipeListLayout.Status.Open) {
                //若有其他的item的状态为Open，则Close，然后移除
                if (sets.size() > 0) {
                    for (SwipeListLayout s : sets) {
                        s.setStatus(SwipeListLayout.Status.Close, true);
                        sets.remove(s);
                    }
                }
                sets.add(slipListLayout);
            } else {
                if (sets.contains(slipListLayout))
                    sets.remove(slipListLayout);
            }
        }

        @Override
        public void onStartCloseAnimation() {

        }

        @Override
        public void onStartOpenAnimation() {

        }

    }

    @Override
    public void onClick(View view) {
        int position = Integer.parseInt(view.getTag().toString());
        switch (view.getId()) {
            case R.id.tv_top:
                callBack.clickAdd(position);
                break;
            case R.id.tv_delete:
                operationAdress(position, true);
                break;
        }
    }

    public interface CallBack {
        void clickAdd(int i);

    }

    static class ViewHolder {
        @BindView(R.id.tv_top)
        TextView tvTop;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.iv_moren)
        ImageView ivMoren;
        @BindView(R.id.tv_adress)
        TextView tvAdress;
        @BindView(R.id.sll_main)
        SwipeListLayout sllMain;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private void operationAdress(final int position, final boolean isDelete) {
        CommonUtils.showDiaLog(context, "正在删除");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(context));
        map.put("address_id", dataBeans.get(position).getId());
        String url = CommonConstant.setDefault;
        if (isDelete)
            url = CommonConstant.delAddress;
        HttpUtils.postHttp(map, url, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                CommonUtils.dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    if (isDelete) {
                        dataBeans.remove(dataBeans.get(position));
                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                    } else {
                        dataBeans.remove(dataBeans.get(position));
                        Toast.makeText(context, "设置默认成功", Toast.LENGTH_SHORT).show();
                    }
                    notifyDataSetChanged();
                } else {
                    Toast.makeText(context, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void errorHttp(String msg) {
                CommonUtils.dismissDiaLog();
            }
        });
    }
}
